//app.js
import {ApiRootUrl} from '/config/index.js'
import Toast from '/@vant/weapp/toast/toast';
App({
  onLaunch: function () {
    // 展示本地存储能力
    Array.prototype.remove = function(b) {
      var a = this.indexOf(b);
      if (a >= 0) {
      this.splice(a, 1);
      return true;
      }
      return false;
      };
    let that = this
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    // console.log("---------init")
    // let openId = wx.getStorageSync('openId')
   
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  checkRegister() {
    let self = this;
    let openId = wx.getStorageSync("openId");
    let url = `${ApiRootUrl}/index/checkAppRegisterUser/${openId}`;
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res) {
        console.log("res==11>",res)
        Toast.clear();
        if (res.data.code == 20000) {
          let data = res.data.data;
          wx.setStorageSync('userinfo', res.data.data)
          console.log("data.user===>", data)
          // 无user表示是劳务工
          if (!data.user) {
            wx.redirectTo({
              url: '/pages/firstpage/firstpage',
            })
          } else {
            // 有user表示是非劳务工
            if (data.user.typeCode == 'business') {
              wx.redirectTo({
                url: '/pages/companyfirstpage/companyfirstpage',
              })
            } else if (data.user.typeCode == 'labour') {
              wx.redirectTo({
                url: '/pages/laborcompany/firstpage/firstpage',
              })
            }
          }
        } else if (res.data.code == 30000) {
          // self.setData({
          //   tishiShow: true
          // })
          // wx.showModal({
          //   title: '提示',
          //   showCancel:false,
          //   content: '会话结束，请重新进入小程序',
          //   success (res) {
          //     if (res.confirm) {
          //       console.log('用户点击确定')
          //     } else if (res.cancel) {
          //       console.log('用户点击取消')
          //     }
          //   }
          // })
        } else if (res.data.code == 30002) {
          // self.initCompany();
          // self.initService();
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  globalData: {
    userInfo: null,
    companyList:[],
    labourInfo:{name:"",code:""},
    answerList:[],
    answer:[],
    score:0,
    errAnswer:[],
    rightAnswer:[]
  }
})